<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>验证邮箱地址唯一性</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <style type="text/css">
        p:not(:empty) {
            padding: 15px;
        }
        
        .container {
            padding-top: 100px;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="form-group">
            <label>邮箱地址</label>
            <input type="email" class="form-control" placeholder="请输入邮箱地址" id="email">
        </div>
        <!-- 错误 bg-danger 正确 bg-success -->
        <p id="info"></p>
    </div>
    <script src="js/ajax.js"></script>
    <script>
        // 获取页面中的元素
        var emailInp = document.getElementById('email');
        var info = document.getElementById('info');

        // 当文本框离开焦点以后
        emailInp.onblur = function() {
            // 获取用户输入的邮箱地址
            var email = this.value;
            // 验证邮箱地址的正则表达式
            var reg = /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d\]+[_.])+[A-Za-z\d]{2,4}$/;
            // 如果用户输入的邮箱地址不符合规则
            if (!reg.test(email)) {
                // 给用户提示 
                info.innerHTML = '请输入符合规则的邮箱地址';
                info.className = 'bg-danger'
                    // 阻止程序继续向下执行
                return;
            }

            // 向服务器端发送请求
            ajax({
                type: 'get',
                url: 'http://localhost:3000/vertifyEmailAdress',
                data: {
                    email: email
                },
                success: function(result) {
                    console.log(result);
                    info.innerHTML = '邮箱地址可用';
                    info.className = 'bg-success';
                },
                error: function(result) {
                    console.log(result);
                    info.innerHTML = '邮箱地址已被注册';
                    info.className = 'bg-danger';
                }
            });

        }
    </script>
</body>

</html>